home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 4.7 KB | 168 lines |
- package symantec.itools.multimedia;
-
-
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
-
- // 01/29/97 TWB Integrated changes from Macintosh
-
- /**
- * MovingAnimation component. Draws a series of images at sequential horizontal
- * positions within the component area. Each frame is drawn at a given offset
- * from the previous frame.
- *
- * @see Animator
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
-
- public class MovingAnimation
- extends Animator
- implements Runnable
- {
- int loopslot;
- int shiftOffset;
- private int curOffset;
-
- /**
- * Constructs a default MovingAnimation.
- */
- public MovingAnimation()
- {
- shiftOffset = 10;
- loopslot = 0;
- curOffset = 0;
- forever = true;
- }
-
- /**
- * Sets the animation shift offset. Each image is shifted horizontally
- * from the previous image by the specified offset. Offset may be position
- * or negative. If the offset is positive, the first image is drawn on the
- * left side of the component and subsequent images are drawn to the right
- * of the first image by offset pixels. If the offset is negative, the
- * first image is drawn at the right side of the component area.
- * @param offset shift offset amount, in pixels
- * @see #getShiftOffset
- */
- public void setShiftOffset(int offset)
- {
- shiftOffset = offset;
- }
-
- /**
- * Gets the animation shift offset.
- * @return int current shift offset amount, in pixels
- * @see #setShiftOffset
- */
- public int getShiftOffset()
- {
- return shiftOffset;
- }
-
- /**
- * Body of MovingAnimation Thread. This method is called by the Java Virtual Machine
- * in response to a call to the start method of this object.
- */
-
- public void run()
- {
- Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1);
-
- Dimension d = size();
- int imageCount = images.size();
-
- if (imageCount > 1)
- {
- if (shiftOffset < 0)
- curOffset = d.width - maxWidth;
-
- int count = 0;
-
- while (true)
- {
- d = size(); //Needed if the component resizes while running.
-
- if (++loopslot >= imageCount)
- {
- if (++count > numLoops && !forever)
- break;
-
- loopslot = 0;
- curOffset += shiftOffset;
-
- if (curOffset < 0)
- curOffset = d.width - maxWidth;
-
- else if (curOffset + maxWidth > d.width)
- curOffset = 0;
- }
-
- repaint();
-
- try
- {
- Thread.sleep(delay);
- }
- catch (InterruptedException e)
- {
- break;
- }
- }
- }
- }
-
- /**
- * Incrementally updates an image as image data becomes available.
- * This is a standard Java AWT method which gets called by the AWT to
- * incrementally draw an image as more image data becomes available.
- * @param img the image being drawn
- * @param flags image update flags (see class ImageObserver)
- * @param x horizontal position
- * @param y vertical position
- * @param w width
- * @param h height
- *
- * @return true if image has been completely loaded
- *
- * @see java.awt.image.ImageObserver
- * @see java.awt.image.ImageObserver#imageUpdate
- */
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
- {
- if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0)
- if ((images != null) && (loopslot < images.size()) && (((AnimatorImage)images.elementAt(loopslot)).image == img))
- repaint(100);
-
- return (flags & (ALLBITS|ERROR)) == 0;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * The horizontal position of the image is shifted according to the current offset.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- if ((images != null) && (loopslot < images.size()))
- {
- Image img = ((AnimatorImage)images.elementAt(loopslot)).image;
- if (img != null)
- g.drawImage(img, curOffset, 0, this);
- }
- }
-
- }
-
-